package com.neusoft.yuejian12.yuejian_12.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.fragment.ProConsultFragment;
import com.neusoft.yuejian12.yuejian_12.fragment.ProCourseFragment;
import com.neusoft.yuejian12.yuejian_12.javabean.AttentionBean;
import com.neusoft.yuejian12.yuejian_12.javabean.IsAttentionBean;
import com.neusoft.yuejian12.yuejian_12.javabean.ProBean;
import com.neusoft.yuejian12.yuejian_12.model.ProModel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.neusoft.yuejian12.yuejian_12.constant.Constant.PROCOURSE_PHOTO_URL;
import static com.neusoft.yuejian12.yuejian_12.constant.Constant.PROFESSOR_PHOTO_URL;

public class ProDetailActivity extends FragmentActivity {

    private ImageView backlist;
    private ImageView collect;
    protected Context context;
    private TextView Protype;
    private ImageView imagePro;
    private TextView Proname;
    private TextView professorinfo;
    private TextView Proinfo;
    private int pmId;
    private String coresult="";
    private int pcId;
    private int ptId;
    private int uId;

    private RadioButton Rbcourse;
    private RadioButton Rbconsult;

    private ProCourseFragment procourseFragment;
    private ProConsultFragment proconsultFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
       // requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pro_detail);
        SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        uId = sp.getInt("u_id",0);
        pmId = getIntent().getIntExtra("pm_id", -1);
        pcId = getIntent().getIntExtra("pc_id", -1);
        ptId = getIntent().getIntExtra("professor_type_id", -1);
        initViews();//初始化控件,页面
        initEvent();//初始化事件
        getProData();
        isBeCollected();
    }

    private void initViews() {
        imagePro = (ImageView) findViewById(R.id.pro_pic);
        collect = (ImageView) findViewById(R.id.collect);
        backlist = (ImageView) findViewById(R.id.back_prolist);
        Proname = (TextView) findViewById(R.id.pro_name);
        Protype = (TextView) findViewById(R.id.pro_type);
        Proinfo = (TextView) findViewById(R.id.pro_info);
        professorinfo = (TextView) findViewById(R.id.professorinfo);

        Rbcourse = (RadioButton) findViewById(R.id.pro_course);
        Rbconsult = (RadioButton) findViewById(R.id.pro_consult);

    }

    private void replaceFragment(Fragment fragment) {
        transaction = fragmentManager.beginTransaction();
        Bundle bundle = new Bundle();
        bundle.putInt("pm_id", pmId);
        fragment.setArguments(bundle);
        transaction.replace(R.id.id_daohang, fragment);
        transaction.commit();
    }

    private void initEvent() {

        backlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // setResult(RESULT_CANCELED);
                finish();
            }
        });
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collectPro();
            }
        });

        pmId=getIntent().getIntExtra("pm_id",-1);

        Rbcourse.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    procourseFragment = new ProCourseFragment();

                    replaceFragment(procourseFragment);
                }
            }
        });

        Rbconsult.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    proconsultFragment = new ProConsultFragment();
                    replaceFragment(proconsultFragment);
                }
            }
        });
        fragmentManager = getSupportFragmentManager();
        Rbcourse.setChecked(true);//设置进入此页显示的Fragment页面

    }
    private void getProData(){
            Bundle bundle = this.getIntent().getExtras();
            int pmid = bundle.getInt("pm_id");
            //  Log.d("m","pm_id");
            String proname = bundle.getString("pm_name");
            String protype = bundle.getString("professor_type_name");
            String proinfo = bundle.getString("pm_info");
            String ProPic = PROFESSOR_PHOTO_URL+bundle.getString("pm_pic");

            Proinfo.setText(proinfo);
            Protype.setText(protype);
            Proname.setText(proname);
            Picasso.with(this).load(ProPic).into(imagePro);
    }

    private void isBeCollected(){
        //判断是否关注
        ProModel proModel=new ProModel();
        Call<IsAttentionBean> callIsCollect=proModel.getIsCollected(uId,pmId,"3");
        callIsCollect.enqueue(new Callback<IsAttentionBean>() {
            @Override
            public void onResponse(Call<IsAttentionBean> call, Response<IsAttentionBean> response) {
                String result=response.body().getCollected();
                if ("0".equals(result)) {

                    collect.setImageResource(R.drawable.ic_collect_hui);
                    coresult = "no";
                } else {
                    coresult = "yes";
                    collect.setImageResource(R.drawable.ic_collect_red);
                }
            }
            @Override
            public void onFailure(Call<IsAttentionBean> call, Throwable t) {

            }
        });

    }


    //关注大师
    private void collectPro(){
        ProModel proModel=new ProModel();
        Call<AttentionBean> callCollect=proModel.userCollectPro(uId,pmId);
        callCollect.enqueue(new Callback<AttentionBean>() {
            @Override
            public void onResponse(Call<AttentionBean> call, Response<AttentionBean> response) {

                if ("yes".equals(coresult)) {
                    Toast.makeText(ProDetailActivity.this, "取消成功", Toast.LENGTH_SHORT).show();
                    collect.setImageResource(R.drawable.ic_collect_hui);
                }
                else{
                    Toast.makeText(ProDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    collect.setImageResource(R.drawable.ic_collect_red);
                }
                refresh();
            }
            @Override
            public void onFailure(Call<AttentionBean> call, Throwable t) {

            }


        });
    }
    private void refresh(){
        isBeCollected();
    }
}
